﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DBUtil;
using ZXCompany.Models.Entity;
using System.Data.SqlClient;
using System.Data;
namespace ZXCompany.Models.Dao
{
    public class ZX_CompanyContactDao
    {
        Database db = new Database();
        string sqlstr = "";
        private List<ZX_CompanyContact> GetListFromDT(DataTable dt)
        {
            List<ZX_CompanyContact> lcontact = new List<ZX_CompanyContact>();
            foreach (DataRow dr in dt.Rows)
            {
                ZX_CompanyContact zccon = new ZX_CompanyContact();
                zccon.addTime = Convert.ToString(dr["addTime"]);
                zccon.addUserID = Convert.ToInt32(dr["addUserID"]);
                zccon.contactType = Convert.ToString(dr["contactType"]);
                zccon.ID = Convert.ToInt32(dr["ID"]);
                zccon.updateTime = Convert.ToString(dr["updateTime"]);
                zccon.xy = Convert.ToString(dr["xy"]);
                lcontact.Add(zccon);
            }
            return lcontact;
        }
        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="sconn"></param>
        /// <returns></returns>
        public List<ZX_CompanyContact> Select(SqlConnection sconn)
        {
            try
            {
                sqlstr = @"SELECT isnull([ID],0)
                                      ,isnull([contactType],'')
                                      ,isnull([xy],'')
                                      ,isnull([addUserID],0)
                                      ,isnull([addTime],'')
                                      ,isnull([updateTime],'')
                                  FROM [ZX_CompanyContact]";
                DataTable dt = db.Squery(sqlstr, sconn);
                return GetListFromDT(dt);
            }
            catch (Exception ex)
            {
                 
                throw ex;
            }
        }

        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="sconn"></param>
        /// <returns></returns>
        public ZX_CompanyContact SelectTop1(SqlConnection sconn)
        {
            try
            {
                sqlstr = @"SELECT top 1 isnull([ID],0) as ID
                                      ,isnull([contactType],'') as contactType
                                      ,isnull([xy],'') as xy
                                      ,isnull([addUserID],0) as addUserID
                                      ,isnull([addTime],'') as addTime
                                      ,isnull([updateTime],'') as updateTime
                                  FROM [ZX_CompanyContact]";
                DataTable dt = db.Squery(sqlstr, sconn);
                ZX_CompanyContact Cctact = new ZX_CompanyContact();
                if (dt==null || dt.Rows.Count<1)
                {
                    Cctact.contactType = "";
                    Cctact.xy = "";
                }
                else
                {
                    Cctact =GetListFromDT(dt)[0];
                }
                return Cctact;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public void Insert(ZX_CompanyContact cinfo, SqlConnection sconn, SqlTransaction stran)
        {
            try
            {
                sqlstr = @"INSERT INTO [ZX_CompanyContact]
                           ([contactType]
                           ,[xy]
                           ,[addUserID]
                           ,[addTime])
                     VALUES
                           (@contactType
                           ,@xy
                           ,@addUserID
                           ,@addTime)";
                SqlParameter[] para = new SqlParameter[]
                {
                    new SqlParameter("@contactType",cinfo.contactType),
                    new SqlParameter("@xy",cinfo.xy),
                    new SqlParameter("@addUserID",cinfo.addUserID),
                    new SqlParameter("@addTime",cinfo.addTime)
                };
                //System.Data.SqlTypes.SqlNullValueException
                try
                {
                    db.Udaquery(sqlstr, sconn,stran, para);
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }
            catch (Exception ex)
            {
                
                throw ex;
            }
        }

        public void Update(ZX_CompanyContact cinfo, SqlConnection sconn, SqlTransaction stran)
        {
           
            sqlstr = @"update [ZX_CompanyContact]
                        set [contactType] =@contactType,
                            [xy] =@xy,updateTime =@updateTime
                        where id=@id";
            SqlParameter[] para = new SqlParameter[]
            {
                new SqlParameter("@contactType",cinfo.contactType),
                new SqlParameter("@xy",cinfo.xy),
                new SqlParameter("@updateTime",cinfo.updateTime),
                new SqlParameter("@id",cinfo.ID)
            };
            try
            {
                db.Udaquery(sqlstr, sconn, stran, para);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }


        public void Delete(ZX_CompanyContact zc, SqlConnection sconn, SqlTransaction stran)
        {
            try
            {

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

    }
}